<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      default: () => ({
        xAxis: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        series: [
          {
            name: '活跃用户',
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: '在线时长',
            data: [220, 182, 191, 234, 290, 330, 310]
          }
        ]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler() {
        this.updateChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.updateChart()
    },
    updateChart() {
      if (!this.chart) return
      
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: 'rgba(0, 0, 0, 0.8)',
              color: '#fff',
              fontSize: 12
            }
          },
          textStyle: {
            fontSize: 14,
            color: '#fff'
          },
          backgroundColor: 'rgba(0, 0, 0, 0.8)',
          borderColor: 'rgba(255, 255, 255, 0.2)',
          borderWidth: 1
        },
        legend: {
          data: this.chartData.series.map(item => item.name),
          textStyle: {
            fontSize: 12,
            color: '#fff',
            fontWeight: 'bold'
          },
          itemWidth: 12,
          itemHeight: 12,
          itemGap: 15
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '8%', // 增加底部空间
          top: 40, // 增加顶部空间
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: this.chartData.xAxis,
            axisLabel: {
              fontSize: 12,
              color: '#fff',
              fontWeight: 'bold'
            },
            axisLine: {
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.3)'
              }
            },
            axisTick: {
              show: false
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              fontSize: 12,
              color: '#fff',
              fontWeight: 'bold'
            },
            axisLine: {
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.3)'
              }
            },
            splitLine: {
              lineStyle: {
                color: 'rgba(255, 255, 255, 0.1)'
              }
            }
          }
        ],
        series: this.chartData.series.map((item, index) => ({
          name: item.name,
          type: 'line',
          stack: 'Total',
          areaStyle: {
            opacity: 0.3,
            color: index === 0 ? '#83bff6' : '#67C23A'
          },
          lineStyle: {
            width: 3,
            color: index === 0 ? '#83bff6' : '#67C23A'
          },
          itemStyle: {
            color: index === 0 ? '#83bff6' : '#67C23A'
          },
          emphasis: {
            focus: 'series',
            itemStyle: {
              borderWidth: 2,
              borderColor: '#fff'
            }
          },
          data: item.data,
          animationDuration
        }))
      }
      
      this.chart.setOption(option)
    }
  }
}
</script>
